package my.rpc.spring;

import my.rpc.service.RpcServer;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

/**
 * 将xml中的registry标签与RpcServer类匹配并交给spring管理
 * @author fengbo
 * @date 2017/11/15
 */
public class RegistryParser implements BeanDefinitionParser {

    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {
        String serviceAddress = element.getAttribute("serviceAddress");

        RootBeanDefinition beanDefinition = new RootBeanDefinition();
        beanDefinition.setBeanClass(RpcServer.class);
        beanDefinition.getPropertyValues().addPropertyValue("serviceAddress", serviceAddress);
        parserContext.getRegistry().registerBeanDefinition("rpcServer", beanDefinition);
        return beanDefinition;
    }
}
